2018_1_30_Time Zones_建table

本文介绍了一个时间转换编程问题,探讨了不同时区之间的转换方法,并提供了一段示例代码来解决从一个时区到另一个时区的时间换算问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

建一个表,注意处理12点

Time Zones
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2586 Accepted: 733

Description

Prior to the late nineteenth century, time keeping was a purely local phenomenon. Each town would set their clocks to noon when the sun reached its zenith each day. A clockmaker or town clock would be the "official" time and the citizens would set their pocket watches and clocks to the time of the town - enterprising citizens would offer their services as mobile clock setters, carrying a watch with the accurate time to adjust the clocks in customer's homes on a weekly basis. Travel between cities meant having to change one's pocket watch upon arrival.
However, once railroads began to operate and move people rapidly across great distances, time became much more critical. In the early years of the railroads, the schedules were very confusing because each stop was based on a different local time. The standardization of time was essential to efficient operation of railroads.

In 1878, Canadian Sir Sanford Fleming proposed the system of worldwide time zones that we use today. He recommended that the world be divided into twenty-four time zones, each spaced 15 degrees of longitude apart. Since the earth rotates once every 24 hours and there are 360 degrees of longitude, each hour the earth rotates one-twenty-fourth of a circle or 15° of longitude. Sir Fleming's time zones were heralded as a brilliant solution to a chaotic problem worldwide.

United States railroad companies began utilizing Fleming's standard time zones on November 18, 1883. In 1884 an International Prime Meridian Conference was held in Washington D.C. to standardize time and select the Prime Meridian. The conference selected the longitude of Greenwich, England as zero degrees longitude and established the 24 time zones based on the Prime Meridian. Although the time zones had been established, not all countries switched immediately. Though most U.S. states began to adhere to the Pacific, Mountain, Central, and Eastern time zones by 1895, Congress didn't make the use of these time zones mandatory until the Standard Time Act of 1918.

Today, many countries operate on variations of the time zones proposed by Sir Fleming. All of China (which should span five time zones) uses a single time zone - eight hours ahead of Coordinated Universal Time (known by the abbreviation UTC - based on the time zone running through Greenwich at 0° longitude). Russia adheres to its designated time zones although the entire country is on permanent Daylight Saving Time and is an hour ahead of their actual zones. Australia uses three time zones - its central time zone is a half-hour ahead of its designated time zone. Several countries in the Middle East and South Asia also utilize half-hour time zones.

Since time zones are based on segments of longitude and lines of longitude narrow at the poles, scientists working at the North and South Poles simply use UTC time. Otherwise, Antarctica would be divided into 24 very thin time zones!

Time zones have recently been given standard capital-letter abbreviations as follows:

UTC Coordinated Universal Time
GMT Greenwich Mean Time, defined as UTC
BST British Summer Time, defined as UTC+1 hour
IST Irish Summer Time, defined as UTC+1 hour
WET Western Europe Time, defined as UTC
WEST Western Europe Summer Time, defined as UTC+1 hour
CET Central Europe Time, defined as UTC+1
CEST Central Europe Summer Time, defined as UTC+2
EET Eastern Europe Time, defined as UTC+2
EEST Eastern Europe Summer Time, defined as UTC+3
MSK Moscow Time, defined as UTC+3
MSD Moscow Summer Time, defined as UTC+4
AST Atlantic Standard Time, defined as UTC-4 hours
ADT Atlantic Daylight Time, defined as UTC-3 hours
NST Newfoundland Standard Time, defined as UTC-3.5 hours
NDT Newfoundland Daylight Time, defined as UTC-2.5 hours
EST Eastern Standard Time, defined as UTC-5 hours
EDT Eastern Daylight Saving Time, defined as UTC-4 hours
CST Central Standard Time, defined as UTC-6 hours
CDT Central Daylight Saving Time, defined as UTC-5 hours
MST Mountain Standard Time, defined as UTC-7 hours
MDT Mountain Daylight Saving Time, defined as UTC-6 hours
PST Pacific Standard Time, defined as UTC-8 hours
PDT Pacific Daylight Saving Time, defined as UTC-7 hours
HST Hawaiian Standard Time, defined as UTC-10 hours
AKST Alaska Standard Time, defined as UTC-9 hours
AKDT Alaska Standard Daylight Saving Time, defined as UTC-8 hours
AEST Australian Eastern Standard Time, defined as UTC+10 hours
AEDT Australian Eastern Daylight Time, defined as UTC+11 hours
ACST Australian Central Standard Time, defined as UTC+9.5 hours
ACDT Australian Central Daylight Time, defined as UTC+10.5 hours
AWST Australian Western Standard Time, defined as UTC+8 hours

Given the current time in one time zone, you are to compute what time it is in another time zone.

Input

The first line of input contains N, the number of test cases. For each case a line is given with a time, and 2 time zone abbreviations. Time is given in standard a.m./p.m. format with midnight denoted "midnight" and noon denoted "noon" (12:00 a.m. and 12:00 p.m. are oxymorons).

Output

For each case, assuming the given time is the current time in the first time zone, give the current time in the second time zone.

Sample Input

4
noon HST CEST 
11:29 a.m. EST GMT
6:01 p.m. CST UTC
12:40 p.m. ADT MSK

Sample Output

midnight
4:29 p.m.
12:01 a.m.
6:40 p.m.

Source

#include<iostream>
#include<algorithm>
#include<string> 
#include<cstring>
#include<vector>
#include<cmath>
#include<fstream>
#include<iomanip>
using namespace std;

ofstream out;
char x[][7] = {
"WET","0",
"UTC","0",
"GMT","0",
"BST","+1",
"IST","+1",
"WEST","+1",
"CET","+1",
"CEST","+2",
"EET","+2",
"EEST","+3",
"MSK","+3",
"MSD","+4",
"AST","-4",
"ADT","-3",
"NST","-3.5",
"NDT","-2.5",
"EST","-5",
"EDT","-4",
"CST","-6",
"CDT","-5",
"MST","-7",
"MDT","-6",
"PST","-8",
"PDT","-7",
"HST","-10",
"AKST","-9",
"AKDT","-8",
"AEST","+10",
"AEDT","+11",
"ACST","+9.5",
"ACDT","+10.5",
"AWST","+8",
"",""};

char s[10];

void getoff(int &time,bool f){
	scanf("%s",s);
	double off;
	int i;
	for(i=0;i<84&&strcmp(s,x[i]);i+=2);
	sscanf(x[i+1],"%lf",&off);
	if(f)
	time-=off*60;
	else time+=off*60;
}
int main(){
	int n;
	cin>>n;
	int time=0;
	int h,m;
	for(int loop=0;loop<n;loop++) {
		scanf("%s",s);
		if(!strcmp(s,"noon"))time=12*60;
		else if(!strcmp(s,"midnight"))time=0;
		else{
			sscanf(s,"%d:%d",&h,&m);
			scanf("%s",s);
			h%=12;
			if(!strcmp(s,"p.m."))h+=12;
			time=h*60+m;
		}
		getoff(time,true);
		getoff(time,false);
		time=(time+24*60)%(24*60);
		if(time==0)
			puts("midnight");
		else if(time==12*60)
			puts("noon");
		else{
			string t;
			if(time<12*60)t=" a.m.";
			else t=" p.m.";
			h=time/60%12;
			if(h==0)h=12;
			m=time%60;
			printf("%d:%02d",h,m);
			cout<<t<<endl;
		}
	}
	return 0;
}

struct nf_conn { /* Usage count in here is 1 for hash table, 1 per skb, * plus 1 for any connection(s) we are `master' for * * Hint, SKB address this struct and refcnt via skb->_nfct and * helpers nf_conntrack_get() and nf_conntrack_put(). * Helper nf_ct_put() equals nf_conntrack_put() by dec refcnt, * beware nf_ct_get() is different and don't inc refcnt. */ struct nf_conntrack ct_general; spinlock_t lock; u16 cpu; #if defined(CONFIG_BCM_KF_NETFILTER) struct bcm_nf_conn_ext bcm_ext; #endif #ifdef CONFIG_NF_CONNTRACK_ZONES struct nf_conntrack_zone zone; #endif /* XXX should I move this to the tail ? - Y.K */ /* These are my tuples; original and reply */ struct nf_conntrack_tuple_hash tuplehash[IP_CT_DIR_MAX]; /* Have we seen traffic both ways yet? (bitset) */ unsigned long status; /* jiffies32 when this ct is considered dead */ u32 timeout; possible_net_t ct_net; #if IS_ENABLED(CONFIG_NF_NAT) struct hlist_node nat_bysource; #endif /* all members below initialized via memset */ struct { } __nfct_init_offset; /* If we were expected by an expectation, this will be it */ struct nf_conn *master; //#if defined(CONFIG_NF_CONNTRACK_MARK) u_int32_t mark; //#endif #ifdef CONFIG_TP_IMAGE u_int32_t wportal_flag; #if defined(CONFIG_TP_AVIRA_PATCH) || defined(CONFIG_TP_SPT_PATCH) u_int8_t type_check_state; #endif #endif #ifdef CONFIG_NF_CONNTRACK_SECMARK u_int32_t secmark; #endif #if defined(CONFIG_NETFILTER_APP_DPI) struct APP_MARK dpi_check_state; #endif #ifdef CONFIG_NETFILTER_APP_DPI /* whether this packet should be skipped by hnat */ __u8 hnat_skip; #endif /* Extensions */ struct nf_ct_ext *ext; /* Storage reserved for other modules, must be the last member */ union nf_conntrack_proto proto; }; 没有start该怎么办?
08-14
<think>根据用户提供的`struct nf_conn`定义,确实没有直接看到`start`字段。这可能是因为内核版本或特定配置的差异。在4.19内核中,`start`字段可能被移除了,或者存在于其他位置(比如扩展结构中)。我们需要通过其他方式获取连接立时间。 ### 解决方案: 1. **检查`timeout`字段**:`u32 timeout`字段通常表示连接超时的绝对时间(jiffies),但这不是立时间。 2. **使用连接跟踪扩展**:连接立时间可能存储在扩展结构中。特别是,时间戳扩展(`nf_ct_tstamp`)可能会包含这个信息。 3. **检查`nf_ct_tstamp`扩展**:在支持时间戳扩展的内核中,可以通过`nf_ct_ext_find(ct, NF_CT_EXT_TSTAMP)`来获取时间戳信息。这个扩展结构包含`start`字段(类型为`ktime_t`)。 ### 步骤: 1. **确认内核配置**:确保内核编译时启用了`CONFIG_NF_CONNTRACK_TIMESTAMP`。如果没有启用,则无法通过此方法获取。 2. **获取时间戳扩展**:在代码中,通过`nf_ct_ext_find`查找`NF_CT_EXT_TSTAMP`扩展,如果存在,则从中读取`start`时间。 ### 代码示例: ```c #include <linux/netfilter/nf_conntrack_timestamp.h> // 包含时间戳扩展定义 void print_connection_time(struct nf_conn *ct) { if (!ct) { printk(KERN_INFO "Invalid nf_conn pointer\n"); return; } // 查找时间戳扩展 struct nf_conn_tstamp *tstamp = nf_conn_tstamp_find(ct); if (tstamp) { ktime_t start_time = tstamp->start; // 获取连接立时间 struct timespec64 ts = ktime_to_timespec64(start_time); printk(KERN_INFO "Connection established at: %lld.%09ld seconds since boot\n", ts.tv_sec, ts.tv_nsec); } else { printk(KERN_INFO "Timestamp extension not available\n"); } } ``` ### 备选方案: - 如果时间戳扩展不可用,可能需要考虑其他方法: - 使用`ct->timeout`结合当前时间推算(不准确,因为`timeout`是超时时间,不是立时间)。 - 在连接立时(如`ipv4_conntrack_in`等函数)自己记录时间(需要修改内核代码)。 ### 注意事项: - **扩展可用性**:时间戳扩展需要内核配置`CONFIG_NF_CONNTRACK_TIMESTAMP=y`,否则`nf_conn_tstamp_find`返回NULL。 - **时间基准**:同样基于系统启动时间(单调递增)。 - **用户空间获取**:如果使用`conntrack`工具,可以通过`conntrack -L`查看,但需要内核支持并导出时间戳。 §§相关问题§§ 1. 如何在内核编译时启用`CONFIG_NF_CONNTRACK_TIMESTAMP`配置选项? 2. 如果时间戳扩展不可用,还有哪些替代方法可以获取连接立时间? 3. 在用户空间如何使用`conntrack`命令查看连接立时间? 4. `nf_conn`结构中的`timeout`字段具体表示什么?它和连接立时间有什么关系? 5. 如何在内核模块中动态检测时间戳扩展是否可用?</think>在您提供的 `struct nf_conn` 定义中确实没有 `start` 字段,这可能是由于内核配置或版本差异导致的。在 Linux 4.19 内核中,连接立时间通常通过时间戳扩展(Timestamp Extension)实现。以下是解决方案: ### 解决方案 1. **启用时间戳扩展**: - 确保内核编译时启用了 `CONFIG_NF_CONNTRACK_TIMESTAMP` 配置选项 - 在 `.config` 文件中确认:`CONFIG_NF_CONNTRACK_TIMESTAMP=y` 2. **访问时间戳扩展**: 时间戳存储在 `nf_conn` 的扩展区域中,通过 `nf_conn_tstamp` 结构体访问: ```c #include <linux/netfilter/nf_conntrack_timestamp.h> void get_connection_time(struct nf_conn *ct) { struct nf_conn_tstamp *tstamp; // 获取时间戳扩展 tstamp = nf_conn_tstamp_find(ct); if (tstamp) { ktime_t start_time = tstamp->start; // 连接立时间 struct timespec64 ts = ktime_to_timespec64(start_time); printk("Connection start: %lld.%09lds\n", ts.tv_sec, ts.tv_nsec); } } ``` 3. **关键函数和结构**: - `nf_conn_tstamp_find()`:获取时间戳扩展指针 - `struct nf_conn_tstamp` 定义: ```c struct nf_conn_tstamp { ktime_t start; // 连接立时间 ktime_t stop; // 连接结束时间 }; ``` ### 验证和调试 1. **检查扩展是否存在**: ```c if (nf_ct_ext_exist(ct, NF_CT_EXT_TSTAMP)) { // 时间戳扩展可用 } ``` 2. **用户空间验证**: ```bash # 检查内核配置 zgrep CONFIG_NF_CONNTRACK_TIMESTAMP /proc/config.gz # 查看连接时间戳(需要支持的工具) conntrack -L -o ktime ``` ### 注意事项 1. 时间戳扩展**默认不启用**,需重新编译内核 2. 时间基准为 `ktime_get_boottime()`(系统启动后的单调时间) 3. 对于 UDP 等无连接协议,`start` 在首个数据包时设置 4. 若扩展不可用,替代方案: ```c u64 start_jiffies = (u64)ct->timeout - nfct_time_stamp; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值